/*
 * Copyright (C) 2023 HiHope Open Source Organization .
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifi_hotspot.h"
#include "lwip/netifapi.h"

#include "net_config.h"

#define CH_NUM   (7)
static struct netif* g_iface = NULL;
static volatile int g_hotspotStarted = 0;
static volatile int g_joinedStations = 0;

static void PrintStationInfo(StationInfo* info)
{
    if (!info) return;
    static char macAddress[32] = {0};
    unsigned char* mac = info->macAddress;
    int ret = snprintf_s(macAddress, sizeof(macAddress), sizeof(macAddress) - 1,
        "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    if (ret < 0) {
        return;
    }
    printf("        PrintStationInfo: STA: mac[%s], disconnectedReason[%d]\n", macAddress, info->disconnectedReason);
}

//static
void OnHotspotStateChanged(int state)
{
    printf("[AP_CB] OnHotspotStateChanged: state[%d]\n", state);
    if (state == WIFI_HOTSPOT_ACTIVE) {
        g_hotspotStarted = 1;
    } else {
        g_hotspotStarted = 0;
    }
}

//static
void OnHotspotStaJoin(StationInfo* info)
{
    g_joinedStations++;
    printf("[AP_CB] OnHotspotStaJoin: active stations[%d]\n", g_joinedStations);
    PrintStationInfo(info);
}

//static
void OnHotspotStaLeave(StationInfo* info)
{
    g_joinedStations--;
    printf("[AP_CB] OnHotspotStaLeave: active stations[%d]\n", g_joinedStations);
    PrintStationInfo(info);
}
/*
static WifiEvent g_eventListener = {
    .OnHotspotStaJoin = OnHotspotStaJoin,
    .OnHotspotStaLeave = OnHotspotStaLeave,
    .OnHotspotStateChanged = OnHotspotStateChanged,
};
*/
WifiErrorCode StartHotspot(void)
{
    WifiErrorCode retCode = WIFI_SUCCESS;
    
    HotspotConfig config = {0};
    strcpy_s(config.ssid, WIFI_MAX_SSID_LEN, AP_SSID);
    strcpy_s(config.preSharedKey, WIFI_MAX_KEY_LEN, AP_PSWD);
    config.securityType = WIFI_SEC_TYPE_PSK;
    config.band = HOTSPOT_BAND_TYPE_2G;
    config.channelNum = CH_NUM;
    
    //retCode = RegisterWifiEvent(&g_eventListener);
    //printf("%s: RegisterWifiEvent: ret[%d]\n", __func__, retCode);

    retCode = SetHotspotConfig(&config);
    printf("%s: SetHotspotConfig:  ret[%d]\n", __func__, retCode);

    g_hotspotStarted = 0;
    retCode = EnableHotspot();
    printf("%s: EnableHotspot: ret[%d]\n", __func__, retCode);

    while (!g_hotspotStarted) {
        usleep(100*1000);
    }
    printf("%s: g_hotspotStarted[%d]\n", __func__, g_hotspotStarted);

    g_iface = netifapi_netif_find("ap0");
    if (g_iface) {
        ip4_addr_t ipaddr;
        ip4_addr_t gateway;
        ip4_addr_t netmask;

        IP4_ADDR(&ipaddr,  192, 168, 1, 1);     /* input your IP for example: 192 168 1 1 */
        IP4_ADDR(&gateway, 192, 168, 1, 1);     /* input your gateway for example: 192 168 1 1 */
        IP4_ADDR(&netmask, 255, 255, 255, 0);   /* input your netmask for example: 255 255 255 0 */
        err_t ret = netifapi_netif_set_addr(g_iface, &ipaddr, &netmask, &gateway);
        printf("%s: netifapi_netif_set_addr:[192.168.1.1] ret[%d]\n", __func__, ret);

        ret = netifapi_dhcps_stop(g_iface); // 海思扩展的HDCP服务接口
        printf("%s: netifapi_dhcps_stop: ret[%d]\n", __func__, ret);

        ret = netifapi_dhcps_start(g_iface, 0, 0); // 海思扩展的HDCP服务接口
        printf("%s: netifapi_dhcp_start: ret[%d]\n", __func__, ret);
    }
    return retCode;
}

WifiErrorCode StopHotspot(void)
{
    WifiErrorCode retCode = WIFI_SUCCESS;
    
    if (g_iface) {
        err_t ret = netifapi_dhcps_stop(g_iface);  // 海思扩展的HDCP服务接口
        printf("%s: netifapi_dhcps_stop: ret[%d]\n", __func__, ret);
    }

    //WifiErrorCode retCode = UnRegisterWifiEvent(&g_eventListener);
    //printf("%s: UnRegisterWifiEvent: ret[%d]\n", __func__, retCode);

    retCode = DisableHotspot();
    printf("%s: EnableHotspot: ret[%d]\n", __func__, retCode);
    
    return retCode;
}